package com.ph.profit.api.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.MessageEnum;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.date.DateUtil;
import com.ph.shopping.common.util.excel.ExcelBean;
import com.ph.shopping.common.util.excel.ExcelUtil;
import com.ph.shopping.common.util.result.MessageInfo;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.verifycode.VerifyUtil;
import com.ph.shopping.facade.permission.vo.SessionUserVo;
import com.ph.shopping.facade.profit.request.AreaReqest;
import com.ph.shopping.facade.profit.request.AuditRequest;
import com.ph.shopping.facade.profit.request.CountyRequest;
import com.ph.shopping.facade.profit.request.HunterProfitRequest;
import com.ph.shopping.facade.profit.request.HunterRequest;
import com.ph.shopping.facade.profit.request.PositionRequest;
import com.ph.shopping.facade.profit.request.TwonRequest;
import com.ph.shopping.facade.profit.service.IProfitService;
import com.ph.shopping.facade.profit.vo.HunterProfitVo;

@Controller
@RequestMapping("api/hunter")
public class HunterController extends BaseController {

	@Reference(version = "1.0.0")
	private IProfitService iProfitService;

	/********************************************* 给北京提供接口 ********************/
	/**
	 * 
	 * @Title: findAreaList
	 * @Description: 获取省市区列表
	 * @author WQiang
	 * @date 2017年4月6日 上午10:36:59
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "findarea", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo findArea(TwonRequest request) {
		try {
			return iProfitService.getAreaList(request.getTownId());
		} catch (Exception e) {
			logger.error("获取省市县列表失败");
			e.printStackTrace();
			return getMessageInfo(MessageEnum.EXCEPTION);
		}
	}

	/**
	 * 
	 * @Title: findTownList
	 * @Description: 获取乡镇列表
	 * @author WQiang
	 * @date 2017年4月6日 上午10:36:47
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "findtownlist", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo findTownList(CountyRequest request) {
		try {
			return iProfitService.getTownList(request.getCountyId());
		} catch (Exception e) {
			logger.error("获取乡镇列表失败");
			return getMessageInfo(MessageEnum.EXCEPTION);
		}
	}

	// 查询省
	@RequestMapping(value = "findprovince", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo findProvince() {
		try {
			return iProfitService.queryProvince();
		} catch (Exception e) {
			e.printStackTrace();
			return getMessageInfo(MessageEnum.EXCEPTION);
		}
	}

	// 查询市
	@RequestMapping(value = "findcity", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo findCity(AreaReqest request) {
		try {
			return iProfitService.queryCity(request.getAreaId());
		} catch (Exception e) {
			e.printStackTrace();
			return getMessageInfo(MessageEnum.EXCEPTION);
		}
	}

	// 查询区、县
	@RequestMapping(value = "findcounty", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo findCounty(AreaReqest request) {
		try {
			return iProfitService.queryCounty(request.getAreaId());
		} catch (Exception e) {
			e.printStackTrace();
			return getMessageInfo(MessageEnum.EXCEPTION);
		}
	}

	/**
	 * 
	 * @Title: addHunterProfit
	 * @Description: 链接北京分润接口
	 * @author WQiang
	 * @date 2017年4月5日 上午10:06:23
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addhunterprofit", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo addHunterProfit(HunterRequest request) {
		try {
			if (VerifyUtil.verifyEntityField(request)) {
				return getMessageInfo(MessageEnum.NO_PARAM);
			}
			return iProfitService.addHunterProfit(request);
		} catch (Exception e) {
			e.printStackTrace();
			return getMessageInfo(MessageEnum.EXCEPTION);
		}
	}

	/**
	 * 
	 * @Title: getProvince
	 * @Description: 查询省
	 * @author WQiang
	 * @date 2017年4月7日 上午11:26:15
	 * @return
	 */
	@RequestMapping(value = "getprovinces", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo getProvince() {
		return iProfitService.getProvinces();
	}

	/**
	 * 
	 * @Title: getCity
	 * @Description: 查询市
	 * @author WQiang
	 * @date 2017年4月7日 上午11:35:11
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getcities", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo getCity(PositionRequest request) {
		try {
			return iProfitService.getCitys(request.getProvinceId());
		} catch (Exception e) {
			logger.error("查询市异常", e.getMessage());
			e.printStackTrace();
			return getMessageInfo(MessageEnum.EXCEPTION);
		}
	}

	/**
	 * 
	 * @Title: getCounty
	 * @Description: 查询区、县
	 * @author WQiang
	 * @date 2017年4月7日 上午11:35:11
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getcounties", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo getCounty(PositionRequest request) {
		try {
			return iProfitService.getCountys(request.getCityId());
		} catch (Exception e) {
			logger.error("查询区县异常", e.getMessage());
			e.printStackTrace();
			return getMessageInfo(MessageEnum.EXCEPTION);
		}
	}

	/**
	 * 
	 * @Title: getTown
	 * @Description: 查询乡镇(社区)
	 * @author WQiang
	 * @date 2017年4月7日 上午11:35:11
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "gettowns", method = RequestMethod.POST)
	@ResponseBody
	public MessageInfo getTown(PositionRequest request) {
		try {
			return iProfitService.getTowns(request.getCountyId());
		} catch (Exception e) {
			logger.error("查询乡镇异常", e.getMessage());
			e.printStackTrace();
			return getMessageInfo(MessageEnum.EXCEPTION);
		}
	}

	/**
	 * 
	 * @Title: toManagerProfit
	 * @Description: 跳转猎头分润页面
	 * @author WQiang
	 * @date 2017年4月12日 下午5:42:39
	 * @return
	 */
	@RequestMapping("tohunterprofit")
	public String toManagerProfit() {
		return "/profit/hunterProfitList";
	}

	/**
	 * 
	 * @Title: listHunterProfit
	 * @Description: 猎头分润列表
	 * @author WQiang
	 * @date 2017年4月12日 下午3:20:00
	 * @param pageBean
	 * @return
	 */
	@RequestMapping("listhunterprofit")
	@ResponseBody
	public Result listHunterProfit(HunterProfitRequest request) {
		try {
			return iProfitService.getHunterProfits(request.getPageNum(), request.getPageSize(), request.getOrderNo());
		} catch (Exception e) {
			e.printStackTrace();
			return getResult(ResultEnum.EXCEPTION);
		}
	}

	/**
	 * 
	 * @Title: auditOper
	 * @Description: 分润审核操作
	 * @author WQiang
	 * @date 2017年4月13日 下午1:38:41
	 * @param request
	 * @return
	 */
	@RequestMapping("auditoper")
	@ResponseBody
	public Result auditOper(AuditRequest request) {
		try {
			SessionUserVo userVo = getLoginUser();
			return iProfitService.audit(request.getProfitId(), request.getStatus(), userVo.getId(),
					request.getTableType(), request.getReason());
		} catch (Exception e) {
			e.printStackTrace();
			return getResult(ResultEnum.EXCEPTION);
		}
	}
	
	/**
	 * 
	 * @Title: exportUnLineManagerProfit
	 * @Description: 导出猎头分润
	 * @author WQiang
	 * @date 2017年4月12日 下午4:03:42
	 * @param request
	 * @param response
	 * @param managerProfit
	 * @return
	 */
	@RequestMapping("exporthunterprofit")
	public Object exportHunterProfit(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<HunterProfitVo> list = (List<HunterProfitVo>) iProfitService.exportHunterProfits("");
			if (VerifyUtil.listIsNotNull(list)) {
				List<Object[]> data = new ArrayList<Object[]>();
				for (HunterProfitVo profit : list) {
					Object[] standard = new Object[30];
					standard[0] = profit.getId();
					standard[1] = profit.getOrderNo();
					standard[2] = profit.getOrderMoney();
					standard[3] = profit.getHunterProfit();
					standard[4] = profit.getPhProfit();
					standard[5] = profit.getChargeProfit();
					standard[6] = profit.getPhChargeProft();
					standard[7] = profit.getYstCHargeProfit();
					standard[8] = profit.getShiName();
					standard[9] = profit.getShiRName();
					standard[10] = profit.getXianName();
					standard[11] = profit.getXianRName();
					standard[12] = profit.getShiProfit();
					standard[13] = profit.getXianProfit();
					standard[14] = profit.getShiRProfit();
					standard[15] = profit.getXianProfit();
					standard[16] = profit.getChargeProfitTotal();
					standard[17] = profit.getChargeProfitRemain();
					standard[18] = profit.getChainProfit();
					standard[19] = profit.getPhChainProfit();
					standard[20] = profit.getYstChainProfit();
					standard[21] = profit.getShiChainProfit();
					standard[22] = profit.getXianChainProfit();
					standard[23] = profit.getChainProfitTotal();
					standard[24] = profit.getChainProfitRemain();
					standard[25] = profit.getAuditor();
					standard[26] = profit.getAuditDate()==null?"":DateUtil.formatDateTime(profit.getAuditDate());
					standard[27] = profit.getAuditStatus();
					standard[28] = profit.getProfitTime()==null?"":DateUtil.formatDateTime(profit.getProfitTime());
					standard[29] = profit.getReason();
					data.add(standard);
				}

				ExcelBean excelbean = new ExcelBean();
				excelbean.setTableHeader(new String[] {"序号","订单编号","订单金额","猎头分润","普惠分润","管理费","普惠管理费","易商通管理费","市级代理名称","市代推广师名称","县级代理名称","县代推广师名称",
						"市级代理管理费分润","县级代理管理费分润","市代推广师分润","县代推广师分润","管理费支出合计","管理费剩余","供应链利润","普惠供应链收入","易商通供应链收入","市级代理供应链分润","县级代理供应链分润",
						"供应链支出合计","供应链剩余","审核人","审核时间","审核状态","分润时间","审核原因"});
				excelbean.setSheetData(data);
				excelbean.setName("猎头分润表");
				ExcelUtil.download(request, response, excelbean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
